package com.lik.javadeep.thread;

import java.util.concurrent.*;

public class ThreadTest {
    private static ExecutorService executorService = Executors.newFixedThreadPool(4);

    public static void whenTest(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());

            int i = 10 / 0;
            System.out.println("运行结果" + i);
            return i;

            // whenComplete 虽然可以得到异常信息，但是无法做出默认响应
        }, executorService).whenComplete((res, exception) -> {
            System.out.println("异步任务结果：" + res + "异常:" + exception);
        }).exceptionally((throwable) -> {
            System.out.println("异常：" + throwable);
            // 可以感知异常，同时返回默认值
            return 0;
        });

        Integer result = future.get();

        executorService.shutdown();

        System.out.println("main end……" + result);

    }

    public static void testThread(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CallableTest callableTest = new CallableTest();
        FutureTask<Integer> integerFutureTask = new FutureTask<>(callableTest);

        Thread thread = new Thread(integerFutureTask);
        thread.start();

        Integer result = integerFutureTask.get();
        System.out.println("主线程获取到结果" + result);
        System.out.println("main end");

        // executorService.
    }

    public static class CallableTest implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程" + Thread.currentThread().getId());

            int i = 10 / 2;
            System.out.println("运行结果" + i);
            return i;
        }
    }

    public static void hanldeTest(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());

            int i = 10 / 1;
            System.out.println("运行结果" + i);
            return i;

            // whenComplete 虽然可以得到异常信息，但是无法做出默认响应
        }, executorService).handle((res, throwable) -> {
            // 正常
            if (res != null) {
                return res * 2;
            }
            // 异常
            if (throwable != null) {
                return 0;
            }
            return 0;
        });

        Integer result = future.get();

        executorService.shutdown();

        System.out.println("main end……" + result);

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 1;
            System.out.println("任务1运行结果" + i);
            return i;
        }, executorService);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("任务2运行结果" + i);
            return i;
        }, executorService);

        // future1.runAfterBoth(future2,()->{
        //     System.out.println("任务三开始");
        //
        // });

        // future1.thenAcceptBoth(future2,(r1,r2)->{
        //     System.out.println("任务三开始");
        //     System.out.println("f1的结果："+r1+";f2的结果"+r2);
        // });

        // thenCombine 允许返回值
        CompletableFuture<String> task3 = future1.thenCombineAsync(future2, (r1, r2) -> {
            return r1 + ":" + r2;
        }, executorService);

        String s = task3.get();


        executorService.shutdown();

        System.out.println("main end……"+s);

    }

}
